package com.example.searchenginebackend.exception;

import com.example.searchenginebackend.common.Result;
import com.example.searchenginebackend.utls.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 * @author 米饭好好吃
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(ServiceException.class)
    public Result handleServiceException(ServiceException e) {
        log.error("接收到业务异常：" + e);
        return ResultUtils.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result handleSystemException(RuntimeException e) {
        return ResultUtils.fail(500, "内部错误！");
    }
}
